//
//  ViewController.m
//  轮播图
//
//  Created by mafeng on 16/7/20.
//  Copyright © 2016年 mafeng. All rights reserved.
//

#import "ViewController.h"
#define WIDTH  [UIScreen mainScreen].bounds.size.width
#define HEIGHT  [UIScreen mainScreen].bounds.size.height
@interface ViewController ()<UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *page;
@property (nonatomic, strong) UIScrollView *scrollView;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    // 数组内存放的是我要现实的轮播图的图片
    NSArray *array = [NSArray arrayWithObjects:@"guidePage_01", @"guidePage_02", @"guidePage_03", @"guidePage_04", nil];
    // 通过scrollView将这些图片添加到上面,从而达到滚动这些图片
    self.scrollView = [[UIScrollView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.scrollView.contentSize = CGSizeMake(WIDTH *(array.count + 2), HEIGHT);
    self.scrollView.pagingEnabled = YES;
    self.scrollView.bounces = NO;
    self.scrollView.delegate = self;
    self.scrollView.showsHorizontalScrollIndicator = NO;
    self.scrollView.showsVerticalScrollIndicator = NO;
    [self.view addSubview:self.scrollView];
    // 在第一张前面加一张图片
     UIScrollView *firstScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        UIImageView *firstImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    firstImgView.image = [UIImage imageNamed:@"guidePage_04"];
    [firstScrollView addSubview:firstImgView];
    [self.scrollView addSubview:firstScrollView];
    // 在最后一张后面加一张图片
     UIScrollView *lastScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * 5, 0, WIDTH, HEIGHT)];
     UIImageView *lastImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    lastImgView.image = [UIImage imageNamed:@"guidePage_01"];
    [lastScrollView addSubview:lastImgView];
    [self.scrollView addSubview:lastScrollView];
    for (int i = 0; i < array.count; i++) {
        UIScrollView *smallScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(WIDTH * (i + 1), 0, WIDTH, HEIGHT)];
        smallScrollView.delegate = self;
        smallScrollView.minimumZoomScale = 0.5;
        smallScrollView.maximumZoomScale = 2;
        UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
        imgView.image = [UIImage imageNamed:array[i]];
        [smallScrollView addSubview:imgView];
        [self.scrollView addSubview:smallScrollView];
    }
    // pageController 分页器
    self.page = [[UIPageControl alloc] initWithFrame:CGRectMake(WIDTH / 2 - 50, HEIGHT - 80, 100, 40)];
    self.page.numberOfPages = 4;
    self.page.currentPage = 0;
    [self.page addTarget:self action:@selector(pageChange:) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:self.page];
     NSLog(@"%@", self.scrollView.subviews);
}
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    if (scrollView != self.scrollView) {
        return [scrollView.subviews firstObject];
    }
    return NULL;
}
- (void)pageChange:(UIPageControl *)page {
    self.scrollView.contentOffset = CGPointMake((page.currentPage + 1) * WIDTH, 0);
}
  // 在scrollView 减速停止之后执行
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    if (scrollView == self.scrollView) {
        if (scrollView.contentOffset.x / WIDTH == 0) {
            self.scrollView.contentOffset = CGPointMake(4 *WIDTH, 0);
        }
        if (scrollView.contentOffset.x / WIDTH == 5) {
            self.scrollView.contentOffset = CGPointMake(WIDTH, 0);
        }
        self.page.currentPage = (scrollView.contentOffset.x / WIDTH) - 1;
        NSLog(@"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
        NSLog(@"%@", self.scrollView.subviews);
        for (UIScrollView *scroll in [self.scrollView subviews]) {
            scroll.zoomScale = 1;
        }
    } else {
       
    }
}
- (void)scrollViewDidZoom:(UIScrollView *)scrollView {
    if (scrollView != self.scrollView) {
        scrollView.center = CGPointMake(scrollView.center.x, HEIGHT / 2);
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
